﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Org.Flowertwig;

namespace SetupExtractor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (!Directory.Exists(folderBrowserDialog1.SelectedPath))
                {
                    Directory.CreateDirectory(folderBrowserDialog1.SelectedPath);
                }

                var fg = new FileGenerator();
                fg.WriteTo(folderBrowserDialog1.SelectedPath);

                var di = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
                var files = di.GetFiles("LocalService.exe");
                if (files.Length > 0)
                {
                    var setupFile = files.FirstOrDefault();
                    if (setupFile != null)
                    {
                        Process.Start(setupFile.FullName);

                        Close();
                        return;
                    }
                }

                MessageBox.Show("Unknown error occured while trying to extract setup files.");
            }
        }
    }
}
